package com.xueyi.aixihu.controller;

import com.xueyi.aixihu.controller.base.BaseController;
import com.xueyi.aixihu.entity.po.AxhFinanceReportCommunity;
import com.xueyi.aixihu.entity.po.AxhFinanceReportVillage;
import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.mapper.FinanceMapper;
import com.xueyi.aixihu.reqeust.FinancePublicListReq;
import com.xueyi.aixihu.reqeust.FinanceReportReq;
import com.xueyi.aixihu.reqeust.FinanceReq;
import com.xueyi.aixihu.reqeust.FindClassifyListByPidReq;
import com.xueyi.aixihu.reqeust.base.PageLimitReq;
import com.xueyi.aixihu.response.DtOneFinanceLogRes;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.service.DtNewsSortService;
import com.xueyi.aixihu.service.IFinanceReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 报表Controller
 *
 * @auther 俞烨
 * @date 2022-08-17 11
 */
@RestController
@Validated
public class FinanceController extends BaseController {


    @Autowired
    IFinanceReportService iFinanceReportService;

    @Autowired
    DtNewsSortService dtNewsSortService;

    @Autowired
    FinanceMapper financeMapper;

    /**
     * 导入报表的Excel信息
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/importFinanceExcel")
    public BaseResponse<Boolean> importFinanceExcel(@NotNull(message = "文件不能为空") MultipartFile file, @NotNull(message = "类型不能为空") @Max(value = 2) @Min(value = 1) Integer financeType) throws IOException, SzException {

        iFinanceReportService.importDataByExcel(file, financeType);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 查询财务审核的数量信息
     *
     * @param financeReq
     * @return
     */
    @RequestMapping("/findAuditFinanceSize")
    public BaseResponse<FinanceAuditSizeVo> findWaitFinanceSize(FinanceReq financeReq) {
        FinanceAuditSizeVo financeSize = iFinanceReportService.findFinanceAuditSize(financeReq);
        return BaseResponse.success(financeSize);
    }

    /**
     * 查询报表的集合和当前状态的数量
     *
     * @param financeReq
     * @return
     */
    @RequestMapping("/findFinanceListAndSizeByReq")
    public BaseResponse<FinanceAuditIndexVo> findFinanceListAndSizeByReq(FinanceReq financeReq) throws SzException {


        List<FinanceAuditVo> financeAuditList = iFinanceReportService.findFinanceAuditList(financeReq);
        Integer financeSizeByStatus = financeReq.getFinanceType() != null ? iFinanceReportService.findFinanceSizeByStatus(financeReq) : CollectionUtils.isEmpty(financeAuditList) ? 0 : financeAuditList.size();
        return BaseResponse.success(new FinanceAuditIndexVo(financeAuditList, financeSizeByStatus));
    }

    /**
     * 查询某个报表的审核进度
     *
     * @param id
     * @return
     */
    @RequestMapping("/findFinanceAuditLog")
    public BaseResponse<List<AxhAuditFinanceLogVo>> findFinanceAuditLog(Integer id) {

        List<AxhAuditFinanceLogVo> auditFinanceLog = iFinanceReportService.findAuditFinanceLog(id);
        return BaseResponse.success(auditFinanceLog);
    }

    /**
     * 查询财务报销的数量
     *
     * @param financeExpenseReq
     * @return
     */
    @RequestMapping("/findFinanceExpenseSizeVo")
    public BaseResponse<FinanceExpenseSizeVo> findFinanceExpenseSizeVo(FinanceExpenseReq financeExpenseReq) {

        return BaseResponse.success(iFinanceReportService.findFinanceExpenseSizeVo(financeExpenseReq));
    }

    /**
     * 查询财务报销的集合和当前状态的数量
     *
     * @param financeReq
     * @return
     */
    @RequestMapping("/findFinanceExpenseListAndSizeByReq")
    public BaseResponse<FinanceAuditIndexVo> findFinanceExpenseListAndSizeByReq(FinanceReq financeReq) throws SzException {


        List<FinanceAuditVo> financeAuditList = iFinanceReportService.findFinanceExpenseList(financeReq);
        Integer financeSizeByStatus = financeReq.isYear() ? CollectionUtils.isEmpty(financeAuditList) ? 0 : financeAuditList.size() : iFinanceReportService.findFinanceExpenseSizeByStatus(financeReq);
        return BaseResponse.success(new FinanceAuditIndexVo(financeAuditList, financeSizeByStatus));
    }

    /**
     * 修改报表的公开状态
     *
     * @param id
     * @param publicStatus
     * @return
     */
    @RequestMapping("/updateFinancePublicStatus")
    public BaseResponse<Boolean> updateFinancePublicStatus(Integer id, Integer publicStatus) throws SzException {

        iFinanceReportService.updateFinancePublicStatus(id, publicStatus);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 提交报表的审核
     *
     * @param id
     * @return
     * @throws SzException
     */
    @RequestMapping("/submitFinance")
    public BaseResponse<Boolean> submitFinance(Integer id) throws SzException {
        iFinanceReportService.submitFinance(id);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 审核报表
     *
     * @param id
     * @param remark
     * @param auditType
     * @return
     * @throws SzException
     */
    @RequestMapping("/auditFinance")
    public BaseResponse<Boolean> auditFinance(@NotNull(message = "参数不合法") Integer id, String remark, @NotNull(message = "参数不合法") Integer auditType) throws SzException {


        iFinanceReportService.auditFinance(id, remark, auditType);

        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 修改用户的报表信息
     *
     * @param id
     * @param month
     * @param financeTitle
     * @return
     * @throws SzException
     */
    @RequestMapping("/updateFinance")
    public BaseResponse<Boolean> updateFinance(@NotNull(message = "参数不合法") Integer id,
                                               @NotBlank(message = "报表月份不能为空") String month,
                                               @NotBlank(message = "报表的标题不能为空") String financeTitle) throws SzException {


        iFinanceReportService.updateFinance(id, financeTitle, month);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 查询报表的详细信息
     *
     * @param financeId
     * @return
     */
    @RequestMapping("/findFinanceInformation")
    public BaseResponse<FinanceDetailVo> findFinanceInformation(@NotNull Integer financeId) throws SzException {

        FinanceDetailVo financeDetailVo = iFinanceReportService.financeDetailVo(financeId);
        return BaseResponse.success(financeDetailVo);
    }

    /**
     * 删除或者恢复报表
     *
     * @param id
     * @param status
     * @return
     * @throws SzException
     */
    @RequestMapping("/delOrRecoverReport")
    public BaseResponse<Boolean> delOrRecoverReport(Integer id, Integer status) throws SzException {
        iFinanceReportService.delOrRecoverReport(id, status);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 上传财务报表的附件信息
     *
     * @param id
     * @param accessory
     * @return
     * @throws SzException
     */
    @RequestMapping("/uploadFinanceAccessory")
    public BaseResponse<Boolean> uploadFinanceAccessory(Integer financeId, Integer lineId, String accessory) throws SzException {
        iFinanceReportService.uploadFinanceAccessory(financeId, lineId, accessory);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 修改或者上传财务报表的某一行的数据
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/updateOrSaveFinanceLineCommunity")
    public BaseResponse<Integer> updateOrSaveFinanceLineCommunity(@Validated AxhFinanceReportCommunity axhFinanceReportCommunity) throws SzException {
        iFinanceReportService.updateOrSaveFinanceLineCommunity(axhFinanceReportCommunity);
        return BaseResponse.success(axhFinanceReportCommunity.getId());
    }


    /**
     * 修改或者上传财务报表的某一行的数据（村）
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/updateOrSaveFinanceLineVillage")
    public BaseResponse<Integer> updateOrSaveFinanceLineVillage(@Validated AxhFinanceReportVillage axhFinanceReportVillage) throws SzException {
        iFinanceReportService.updateOrSaveFinanceLineVillage(axhFinanceReportVillage);
        return BaseResponse.success(axhFinanceReportVillage.getId());
    }


    /**
     * 删除财务报表的一行数据
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/deleteFinanceLine")
    public BaseResponse<Integer> deleteFinanceLine(Integer financeId, Integer lineId) throws SzException {
        iFinanceReportService.deleteFinanceLine(financeId, lineId);
        return BaseResponse.success(1);
    }

    /**
     * 查询财务中心的总计信息
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/findFinanceReportSizeVo")
    public BaseResponse<FinanceReportSizeVo> findFinanceReportSizeVo(FinanceReportReq req) throws SzException {
        return BaseResponse.success(iFinanceReportService.findFinanceReportSizeVo(req));
    }

    /**
     * 查询数据看板的信息
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/findDataBlackList")
    public BaseResponse<DataBlackListVo> findDataBlackList(FinanceReportReq req) throws SzException {
        return BaseResponse.success(iFinanceReportService.findDataBlackList(req));
    }

    /**
     * 查询数据看板的日志信息
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/findFinanceLogList")
    public BaseResponse<List<AxhAuditFinanceLogVo>> findFinanceLogList(FinanceReportReq req) throws SzException {
        return BaseResponse.success(iFinanceReportService.findFinanceLogList(req));
    }

    /**
     * 查询个人操作数据看板的日志信息
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/findOneFinanceLogList")
    public BaseResponse<DtOneFinanceLogRes> findOneFinanceLogList(PageLimitReq pageLimitReq) throws SzException {
        return BaseResponse.success(iFinanceReportService.findOneFinanceLogList(pageLimitReq));
    }

    /**
     * 查询数据分析
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/financeFxEcharts")
    public BaseResponse<List<FinanceFxEchartsVo>> financeFxEchartsVo(FinanceReportReq req) throws SzException {
        return BaseResponse.success(iFinanceReportService.financeFxEchartsVo(req));
    }

    /**
     * 首页账户的信息和村社区的情况
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/indexAccountDetail")
    public BaseResponse<IndexAccountDetail> indexAccountDetail() throws SzException {
        return BaseResponse.success(iFinanceReportService.indexAccountDetail());
    }


    @RequestMapping("/dtClassify/findThreeClassifyById2")
    public BaseResponse<List<SelectVo>> findThreeClassifyById(@Validated FindClassifyListByPidReq findClassifyReq) throws SzException {
        List<SelectVo> threeMenu = dtNewsSortService.findThreeClassifyById(findClassifyReq);
        // 查询自己有多少管辖的id
        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
        String userManageAreaId = currentUser.findUserManageAreaId();
        String parentIds = "3,16";
        Integer userMaxRole = currentUser.findUserMaxRole();
        if (userMaxRole.equals(1) || userMaxRole.equals(3) || userMaxRole.equals(5)) {
            parentIds = "3";
        } else if (userMaxRole.equals(2) || userMaxRole.equals(4) || userMaxRole.equals(6)) {
            parentIds = "16";
        }
        if (StringUtils.isEmpty(userManageAreaId)) {
            userManageAreaId = parentIds;
        } else {
            userManageAreaId += ",";
            userManageAreaId += parentIds;
        }
        Map<String, Integer> addMenuMap = new HashMap<>();
        List<SelectVo> threeMenuTemp = new ArrayList<>();
        if (!StringUtils.isEmpty(userManageAreaId) && !CollectionUtils.isEmpty(threeMenu)) {
            String[] split = userManageAreaId.split(",");
            for (SelectVo menu : threeMenu) {
                for (String s : split) {
                    if ((menu.getId() + "").equals(s) && !addMenuMap.containsKey(menu.getId() + "")) {
                        addMenuMap.put(menu.getId() + "", 1);
                        threeMenuTemp.add(menu);
                    }
                }
            }
        }
        return BaseResponse.success(threeMenuTemp);
    }

    /**
     * 查询访问量
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/findReqSizeLineReport")
    public BaseResponse<List<LineReportVo>> findReqSizeLineReport(Integer type, String month) throws SzException {
        return BaseResponse.success(iFinanceReportService.findReqSizeLineReport(type, month));
    }


    /**
     * 查询公共地址
     *
     * @param type
     * @return
     */
    @RequestMapping("/findPublicSort")
    public BaseResponse<List<DataBlackListVo.BlackList>> findNewSort(String type, Integer isPublic) {

        List<DataBlackListVo.BlackList> financeMapperSort = financeMapper.findSort(type);
        // 判断有没有登录

        if ((isPublic == null || isPublic != 1) && !CollectionUtils.isEmpty(financeMapperSort)) {
            DtUsersVo currentUser = DtUsersVo.findCurrentUser();
            List<DataBlackListVo.BlackList> financeMapperSortTemp = new ArrayList<>();
            String[] split = currentUser.findUserManageAreaId().split(",");
            for (DataBlackListVo.BlackList blackList : financeMapperSort) {
                for (String s : split) {
                    if ((blackList.getId() + "").equals(s)) {
                        financeMapperSortTemp.add(blackList);
                    }
                }
            }

            financeMapperSort = financeMapperSortTemp;
        }
        return BaseResponse.success(financeMapperSort);
    }

    /**
     * 转换成草稿的状态
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/convertToDraft")
    public BaseResponse<Boolean> convertToDraft(@NotNull Integer financeId) throws SzException {
        iFinanceReportService.convertToDraft(financeId);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 查询审核的数据信息
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/findAuditTotal")
    public BaseResponse<AuditTotalVo> findAuditTotal(@NotNull Integer areaType) throws SzException {
        AuditTotalVo auditTotal = iFinanceReportService.findAuditTotal(areaType);
        return BaseResponse.success(auditTotal);
    }

    /**
     * 查询财务报表的附件信息
     *
     * @return
     * @throws SzException
     */
    @RequestMapping("/findFinanceAccessory")
    public BaseResponse<List<String>> findFinanceAccessory(@NotNull Integer financeId, @NotNull Integer lineId) throws SzException {

        return BaseResponse.success(iFinanceReportService.findFinanceAccessory(financeId, lineId));
    }

    /**
     * 查询公开的财务列表
     *
     * @param publicListReq
     * @return
     */
    @RequestMapping("/findPublicFinanceList")
    public BaseResponse<List<FinanceAuditVo>> findPublicFinanceList(FinancePublicListReq publicListReq) {
        List<FinanceAuditVo> publicFinanceList = iFinanceReportService.findPublicFinanceList(publicListReq);
        return BaseResponse.success(publicFinanceList);
    }

    /**
     * 查询审核记录
     * <p>
     * type 1报表 2文章
     */
    @RequestMapping("/findAuditLogRecord")
    public BaseResponse<List<AuditLogRecord>> findAuditLogRecord(Integer financeId, Integer type) {
        if (null == type) {
            type = 1;
        }
        return BaseResponse.success(iFinanceReportService.findAuditLogRecord(financeId, type));
    }

    /**
     * 清空副本的信息
     *
     * @param financeId
     * @param financeType
     * @return
     */
    @RequestMapping("/clearFb")
    public BaseResponse<Boolean> clearFb(Integer financeId, Integer financeType) throws SzException {

        iFinanceReportService.clearFb(financeId, financeType);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 提交副本的信息进行审核
     *
     * @param financeId
     * @param financeType
     * @return
     */
    @RequestMapping("/submitFb")
    public BaseResponse<Boolean> submitFb(Integer financeId, Integer financeType) throws SzException {

        iFinanceReportService.submitFb(financeId);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 是否为领导
     *
     * @return
     */
    @RequestMapping("/isLead")
    public BaseResponse<Boolean> isLead() {
        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
        Integer userMaxRole = currentUser.findUserMaxRole();
        return BaseResponse.success(userMaxRole.equals(DtUsersVo.roleIdList.get(6)) || userMaxRole.equals(DtUsersVo.roleIdList.get(7)));
    }


    /**
     * 查看手机端的权限
     *
     * @return
     */
    @RequestMapping("/phone/role")
    public BaseResponse<PhoneRole> phoneRole() {
        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
        PhoneRole phoneRole = new PhoneRole();
        Integer userMaxRole = currentUser.findUserMaxRole();
        if (userMaxRole > 2) {
            phoneRole.setAuditRole(1);
        }
        if (userMaxRole == 1 || userMaxRole == 2) {
            phoneRole.setFinanceRole(1);
        }

        return BaseResponse.success(phoneRole);
    }


    @RequestMapping("/send/notify")
    public BaseResponse<Boolean> sendNotify() throws SzException {
        iFinanceReportService.findNotifyList();
        return BaseResponse.success(Boolean.TRUE);
    }
}


